package cc.core.view.jusfan;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.AsyncTask;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;


public class CstSwitch extends View {

    private int viewHeight, viewWidth;
    private RectF rectf, thumbRectf;
    private Paint openPaint, thumbPaint, closePaint;
    private float density, downx;
    private CheckListener mListener;
    private boolean check;
    private boolean canTouch;
    private boolean callBack;
    protected int speed;


    public CstSwitch(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    public CstSwitch(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CstSwitch(Context context) {
        super(context);
        init(context);
    }

    private void init(Context context) {
        openPaint = new Paint();
        thumbPaint = new Paint();
        closePaint = new Paint();
        speed = 15;
        check = true;
        canTouch = true;
        callBack = true;
        closePaint.setAntiAlias(true);
        thumbPaint.setAntiAlias(true);
        closePaint.setARGB(255, 226, 225, 228);
        thumbPaint.setARGB(255, 255, 255, 255);
        openPaint.setAntiAlias(true);
        density = context.getResources().getDisplayMetrics().density;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (rectf == null && thumbRectf == null) {
            rectf = new RectF(0f, 0f, viewWidth, viewHeight);
            thumbRectf = new RectF(check ? viewWidth / 2f : 2f, 2f, check ? viewWidth - 2f : viewWidth
                    / 2, viewHeight - 2);
        }


        canvas.drawRoundRect(rectf, 5 * density, 5 * density, closePaint);

        if (thumbRectf.left > 2) {
            openPaint.setARGB(255, 27, 177, 161);
            openPaint.setAlpha((int) (255 * (thumbRectf.left - 2) * 2 / viewWidth));
            canvas.drawRoundRect(rectf, 5 * density, 5 * density, openPaint);
        } else {
            openPaint.setARGB(0, 0, 0, 0);
            canvas.drawRoundRect(rectf, 5 * density, 5 * density, openPaint);
        }

        canvas.drawRoundRect(thumbRectf, 5 * density, 5 * density, thumbPaint);
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        this.canTouch = enabled;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (!canTouch) return false;

        switch (event.getAction()) {
            case MotionEvent.ACTION_UP:
                if (check) {
                    new A().execute(-speed);
                } else {
                    new A().execute(speed);
                }

                break;

            case MotionEvent.ACTION_MOVE:
                float dis = event.getX() - downx;
                if ((thumbRectf.left > 2f || (thumbRectf.left == 2f && dis > 0)) && (thumbRectf.left <
                        viewWidth / 2 || (thumbRectf.left == viewWidth / 2 && dis < 0))) {
                    thumbRectf.left = thumbRectf.left + dis;
                    thumbRectf.right = thumbRectf.left + viewWidth / 2;
                    invalidate();
                } else if (thumbRectf.left < 2f) {
                    thumbRectf.left = 2f;
                    thumbRectf.right = viewWidth / 2f;
                    invalidate();
                } else if (thumbRectf.left >= viewWidth / 2) {
                    thumbRectf.left = viewWidth / 2f;
                    thumbRectf.right = viewWidth - 2f;
                    invalidate();
                }
                downx = event.getX();
                break;
            case MotionEvent.ACTION_DOWN:
                downx = event.getX();
                break;

            default:
                break;
        }
        return true;
    }

    public void setCheckListener(CheckListener checkListener) {
        mListener = checkListener;
    }

    public void setThumbColor(int color) {
        thumbPaint.setColor(color);
    }

    public void setCloseDrawableColor(int color) {
        closePaint.setColor(color);
    }

    public void setOpenDrawableColor(int color) {
        openPaint.setColor(color);
    }

    public void setCheck(boolean check) {
        setCheck(check, true);
    }

    public void setCheck(boolean check, boolean callBack) {
        this.callBack = callBack;
        this.check = check;
        if (thumbRectf != null) {
            new A().execute(check ? speed : -speed);
        }
    }

    public void setCheck(boolean check, CheckListener checkListener) {
        this.mListener = checkListener;
        this.check = check;
        if (thumbRectf != null) {
            new A().execute(check ? speed : -speed);
        }
    }

    public boolean getCheck() {
        return this.check;
    }

    public interface CheckListener {
        void check(int id, boolean isOpened);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        viewHeight = this.getHeight();
        viewWidth = this.getWidth();
    }

    class A extends AsyncTask<Integer, Void, Void> {

        @Override
        protected Void doInBackground(Integer... params) {
            if (params[0] > 0) {
                while (thumbRectf.left < viewWidth / 2f) {
                    if (thumbRectf.left < viewWidth / 2f) {
                        thumbRectf.left += params[0];
                        thumbRectf.right = thumbRectf.left + viewWidth / 2f;
                    } else {
                        thumbRectf.left = viewWidth / 2f;
                        thumbRectf.right = viewWidth - 2f;
                    }
                    publishProgress();
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                thumbRectf.left = viewWidth / 2f;
                thumbRectf.right = viewWidth - 2f;
                publishProgress();
            }

            if (params[0] < 0) {
                while (thumbRectf.left > 2f) {
                    if (thumbRectf.left > 2f) {
                        thumbRectf.left += params[0];
                        thumbRectf.right = thumbRectf.left + viewWidth / 2f;
                    } else {
                        thumbRectf.left = 2f;
                        thumbRectf.right = viewWidth / 2f;
                    }
                    publishProgress();
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                thumbRectf.left = 2f;
                thumbRectf.right = viewWidth / 2f;
                publishProgress();
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
            invalidate();
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            check = thumbRectf.left > viewWidth / 2 || thumbRectf.left == viewWidth / 2;
            if (mListener != null && callBack) {
                mListener.check(getId(), check);
            }
            callBack = true;
        }

    }

}
